//
//  ForgotPasswordViewController.m
//  ksjv2
//
//  Created by mac on 14/12/18.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "TermsViewController.h"
#import "Util.h"
@interface ForgotPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *Verification;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *VerificationButton;//验证
@property (weak, nonatomic) IBOutlet UIButton *next;//下一步、注册
@property (weak, nonatomic) IBOutlet UIButton *backButton;//已有帐号

@property (weak, nonatomic) IBOutlet UIButton *IamAgree;

@property (weak, nonatomic) IBOutlet UIButton *watchTheTerms;

@end

@implementation ForgotPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=UIColorFromRGB(0xf5f5f5);
    
    [_IamAgree setImage:[UIImage imageNamed:@"dyNotGou"] forState:UIControlStateNormal];
    
    [_IamAgree setImage:[UIImage imageNamed:@"dyGou"] forState:UIControlStateSelected];
    
    [_IamAgree setSelected:YES];
    
    [self.VerificationButton addTarget:self action:@selector(VerificationActionF) forControlEvents:UIControlEventTouchUpInside];
    [self.VerificationButton setTag:0];
    
    self.title=@"注册";
    [self.next setTitle:@"注册" forState:UIControlStateNormal];
    [self.next addTarget:self action:@selector(registered) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 22, 22)];
    [button addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        
        [button setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        
    }

     UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem=leftBarItem;
    // Do any additional setup after loading the view.
}
-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)VerificationActionF{
    if (self.phone.text.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的手机号码"];
        return;
    }
    if (self.phone.text.length!=11||![CommonHelper isValidatePhone:self.phone.text]) {
        [SVProgressHUD showErrorWithStatus:@"手机号码格式错误"];
        return;
    }
    [self.VerificationButton setTag:30];
    [self VerificationA];
    [self.VerificationButton setBackgroundColor:[UIColor lightGrayColor]];
    
    [[AppAPIClient sharedClient] GET:@"User/VerifyMobile" parameters:@{@"mobile":self.phone.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        int code = [responseObject[@"code"] intValue];
        //            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        if(code == 1){
            //                [alert setMessage:@"注册成功！"];
            [SVProgressHUD showSuccessWithStatus:@"验证码发送成功"];
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"验证码发送失败"];
        }
        
        NSLog(@"%@",responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"验证码错误"];
        NSLog(@"Error:%@ => %@",error,operation.responseString);
    }];
}
-(void)VerificationA{
    [self.VerificationButton setEnabled:YES];
    if (self.VerificationButton.tag==0) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(VerificationA) object:nil];
        [self.VerificationButton setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.VerificationButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ico02.png"]]];
        return;
    }
    
    self.VerificationButton.tag-=1;
    [self.VerificationButton setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.VerificationButton.tag] forState:UIControlStateNormal];
    self.VerificationButton.titleLabel.text=[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.VerificationButton.tag];
    [self performSelector:@selector(VerificationA) withObject:nil afterDelay:1];
    [self.VerificationButton setEnabled:NO];
}
- (void)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resignFirst];
}
-(void)resignFirst{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
//-(void)setIsForgotPassword:(BOOL)isForgotPassword{
//    if (isForgotPassword) {
//        self.title=@"找回密码";
//        [self.next setTitle:@"下一步" forState:UIControlStateNormal];
//        [self.next addTarget:self action:@selector(forgotPassword) forControlEvents:UIControlEventTouchUpInside];
//        self.Verification.placeholder=@"  输入您的邮箱";
//        for (UIView*view in self.view.subviews) {
//            if (view.tag==11) {
//                view.hidden=YES;
//            }
//        }
//    }else{
//        
//        
//    }
//}
//找加密码
-(void)forgotPassword{
//    if (![CommonHelper isValidatePhone:self.phone.text]) {
//        return;
//    }
    
}
//注册
-(void)registered{
//    if (![CommonHelper isValidatePhone:self.phone.text]) {
//        return;
//    }
    
    if (_IamAgree.selected == NO) {
        [SVProgressHUD showErrorWithStatus:@"未同意服务条款"];
        return;
    }
    else if (self.phone.text.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的手机号码"];
        return;
    }
    
    else if (self.phone.text.length!=11) {
        [SVProgressHUD showErrorWithStatus:@"手机号码格式错误"];
        return;
    }else if (self.Verification.text.length<4) {
        [SVProgressHUD showErrorWithStatus:@"验证错误"];
        return;
    }else if (self.password.text.length==0) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return;
    }
    [self resignFirst];
    
            if([self.phone.text length] == 11 &&self.password.text.length>=6){
                SHOW_LOADING;

                [[AppAPIClient sharedClient] GET:@"User/VerifyMobile" parameters:@{@"mobile":self.phone.text,@"password":self.password.text,@"code":self.Verification.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    int code = [responseObject[@"code"] intValue];
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
                    //[alert setTitle:@"提示"];
                    if(code == 1){
                        [alert setMessage:@"注册成功！"];
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                    else if (code == 0){
                    
                    [alert setMessage:@"注册失败！"];
                    }
                    else{
                        if ([responseObject[@"message"] isEqualToString:@"-1"]) {
                            [alert setMessage:@"用户名已经存在"];
                        }else if ([responseObject[@"message"] isEqualToString:@"-2"]){
                            [alert setMessage:@"email已存在"];
                        }else if ([responseObject[@"message"] isEqualToString:@"-3"]){
                            [alert setMessage:@"email格式错误"];
                        }else if ([responseObject[@"message"] isEqualToString:@"-4"]){
                            [alert setMessage:@"用户名禁止注册"];
                        }else if ([responseObject[@"message"] isEqualToString:@"-5"]){
                            [alert setMessage:@"邮箱禁止注册"];
                        }
                        
                    }
                    [alert show];
                    NSLog(@"%@",responseObject);
//                    [SVProgressHUD showSuccessWithStatus:LOADING_TEXT_SUCCESS];
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
                    NSLog(@"Error:%@ => %@",error,operation.responseString);
                }];
            }else{
                if ([self.phone.text length] !=11||![CommonHelper isValidatePhone:self.phone.text]) {
                    SHOW_ALERT(@"提示", @"手机号码格式错误");
                } else if(self.password.text.length<6){
                    SHOW_ALERT(@"提示", @"密码不能少于六位");
                }
                
         }

}

- (IBAction)agrreEvent:(id)sender {
    
     _IamAgree.selected =! _IamAgree.selected;
}

- (IBAction)goToSeeTheTerms:(id)sender {
    
    UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController*login=[story instantiateViewControllerWithIdentifier:@"TermsViewController"];
    [self.navigationController pushViewController:login animated:YES];

}


@end
